package io.aicn.lowcode.controller;

import io.aicn.lowcode.generator.independent.IndependentCodeGenerator;
import io.aicn.lowcode.generator.independent.model.IndependentGenerationRequest;
import io.aicn.lowcode.generator.independent.model.IndependentGenerationResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "独立代码生成器", description = "不依赖MyBatis Plus的独立代码生成器API")
@RestController
@RequestMapping("/api/independent-generator")
public class IndependentCodeGeneratorController {

    @Autowired
    private IndependentCodeGenerator independentCodeGenerator;

    @Operation(summary = "生成独立代码", description = "生成不依赖MyBatis Plus的完整CRUD代码")
    @PostMapping("/generate")
    public IndependentGenerationResult generateCode(@RequestBody IndependentGenerationRequest request) {
        return independentCodeGenerator.generateCode(request);
    }

    @Operation(summary = "获取代码生成配置示例", description = "获取代码生成器的配置示例")
    @GetMapping("/config-example")
    public IndependentGenerationRequest getConfigExample() {
        IndependentGenerationRequest request = new IndependentGenerationRequest();
        request.setTableName("user");
        request.setBasePackage("com.example.demo");
        request.setOutputDir("./src/main/java");
        request.setAuthor("Developer");
        request.setGenerateTests(true);

        IndependentGenerationRequest.IndependentEntityConfig entityConfig = new IndependentGenerationRequest.IndependentEntityConfig();
        entityConfig.setClassName("User");
        entityConfig.setTableName("user");
        entityConfig.setTableComment("用户表");
        entityConfig.setLombok(true);
        entityConfig.setSwagger(true);
        request.setEntityConfig(entityConfig);

        IndependentGenerationRequest.IndependentMapperConfig mapperConfig = new IndependentGenerationRequest.IndependentMapperConfig();
        mapperConfig.setClassName("UserMapper");
        mapperConfig.setEntityClassName("User");
        mapperConfig.setTableName("user");
        mapperConfig.setXmlEnabled(true);
        request.setMapperConfig(mapperConfig);

        IndependentGenerationRequest.IndependentServiceConfig serviceConfig = new IndependentGenerationRequest.IndependentServiceConfig();
        serviceConfig.setClassName("UserServiceImpl");
        serviceConfig.setInterfaceName("UserService");
        serviceConfig.setEntityClassName("User");
        serviceConfig.setMapperClassName("UserMapper");
        serviceConfig.setInterfaceEnabled(true);
        request.setServiceConfig(serviceConfig);

        IndependentGenerationRequest.IndependentControllerConfig controllerConfig = new IndependentGenerationRequest.IndependentControllerConfig();
        controllerConfig.setClassName("UserController");
        controllerConfig.setEntityClassName("User");
        controllerConfig.setServiceClassName("UserService");
        controllerConfig.setRestStyle(true);
        controllerConfig.setEnableSwagger(true);
        controllerConfig.setApiPath("/api/users");
        request.setControllerConfig(controllerConfig);

        return request;
    }
}